<template>
	<view>
		<scroll-view scroll-y class="page">
			<!-- 头部 -->
			<cu-custom bgColor="bg-rgb" :isBack="true">
				<block slot="backText">返回</block>
				<block slot="content">辅料领用项目</block>
				<view slot="right" @tap="rightClick">凭证抬头</view>
			</cu-custom>
			<!-- 查询条件 -->
			<view class="fixed" :style="[{animationDelay: 0.1 + 's'}]">
				<form>
					<view class="cu-form-group last">
						<image class="icon" src="../../static/saomiao.png" mode=""></image>
						<input placeholder="请扫描物料编号" name="input" v-model="myFormData.oddNumber" @confirm="inputQuery"></input>
					</view>
				</form>
			</view>
			<!-- 抬头凭证 输入弹窗 -->
			<view class="cu-modal" :class="modalName=='Modal'?'show':''">
				<view class="cu-dialog">
					<view class="cu-bar bg-white justify-end">
						<view class="content">凭证抬头</view>
						<view class="action">
							<text class="cuIcon-close text-red" @click="close"></text>
						</view>
					</view>
					<view class="padding-xl">
						<form>
							<view class="cu-form-group">
								<view class="title">凭证日期：</view>
								<picker mode="date" :value="myModelData.cerDate" @change="cerDateChange">
									<view class="picker">
										{{myModelData.cerDate}}
									</view>
								</picker>
							</view>
							<view class="cu-form-group">
								<view class="title">过账日期：</view>
								<picker mode="date" :value="myModelData.accountDate" @change="accountDateChange">
									<view class="picker">
										{{myModelData.accountDate}}
									</view>
								</picker>
							</view>
							<view class="cu-form-group">
								<view class="title">类型：</view>
								<input placeholder="工号" style="text-align: right;" name="input" v-model="myModelData.affairType" disabled></input>
							</view>
							<view class="cu-form-group">
								<view class="title">描述：</view>
								<textarea value="" placeholder="请输入描述" style="text-align: left;text-indent: 2em;" v-model="myModelData.affairContent" />
								</view>
							<view class="padding flex flex-direction">
								<button class="cu-btn lg"  @click="onSubmit">确定</button>
							</view>
						</form>
					</view>
				</view>
			</view>
			<!-- 数据列表 -->
			<view class="list">
				<view class="card animation-slide-bottom" v-for="(item,index) in list" :key="index" 
				 :style="[{animationDelay: (index + 1)*0.1 + 's'}]">
					<view class="top">
						<view class="left">物料编号：<text>{{item.materielCode}}</text></view>
						<view class="right">{{item.mobileCode}}</view>
					</view>
					<view class="line"></view>
					<view class="bottom">
						<view class="box">
							<view class="lable">
								物料名称:
							</view>
							<view class="content">
								{{item.materielName}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								移动类型:
							</view>
							<view class="content">
								{{item.mobileType}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								预留编号:
							</view>
							<view class="content">
								{{item.reserveCode}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								存储位置:
							</view>
							<view class="content">
								{{item.storageSite}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								录入项单位:
							</view>
							<view class="content">
								{{item.inputUnit}}
							</view>
						</view>
						<view class="box">
							<view class="lable">
								录入数量:
							</view>
							<view class="content">
								{{item.inputNum}}
							</view>
						</view>
						<view class="box">
							<view class="lable numName" style="background-color: #99CC00;">
								退库数量:
							</view>
							<view class="content numInput">
								<input style="width: 260rpx;" type="text" v-model="preservationList[index].num" value="" placeholder="请输入发料数量" />
							</view>
						</view>
					</view>
					<view class="line"></view>
					<view class="btns">
						<view class="btn1" @click="Withdrawal(index)">退库</view>
						<view class="btn2" @click="scanShipment(index)">扫描退库</view>
					</view>
				</view>
			</view>
			<view class="none" v-if="noDataAvailable">
				<view class="none1">
					暂无数据
				</view>
			</view>
			<view class="none2" v-if="noMoreDataAvailable">
				没有更多数据
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		name: 'returnOfAuxiliaryMaterialsDetail',
		data() {
			return {
				id:'',
				myFormData: {
					oddNumber: ''
				},
				list: [],
				preservationList:[],
				pageNo: 1,
				noDataAvailable: false,
				noMoreDataAvailable: false,
				// 控制凭证抬头显示 隐藏
				modalName:'',
				// 凭证抬头 绑定值
				myModelData:{
					cerDate:'', //凭证日期
					accountDate:'', // 过账日期
					affairType:'',
					affairContent:''
				},
			}
		},
		onLoad(e) {
			let date = new Date()
			this.myModelData.cerDate = date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate();
			this.myModelData.accountDate = date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate();
			this.myModelData.affairType = uni.getStorageSync('affairType');
			this.id = uni.getStorageSync('returnOfAuxiliaryMaterialsld');
			this.getMesCertificatePerkList();
		},
		onPullDownRefresh: function() {
			let _this = this
			_this.pageNo = 1
			_this.list = []
			setTimeout(function() {
				uni.stopPullDownRefresh();
				_this.getMesCertificatePerkList()
			}, 1000);
		},
		onReachBottom: function() {
			let _this = this
			if (this.noDataAvailable == true || this.noMoreDataAvailable == true) {
				return
			}
			_this.pageNo++
			_this.getMesCertificatePerkList()
		},
		methods: {
			// 物料凭证项目-通过主表ID查询
			getMesCertificatePerkList() {
				uni.showLoading({
					mask:true,
					title:'加载中.....'
				})
				this.noDataAvailable = false
				this.noMoreDataAvailable = false
				let data = {
					pageNo: this.pageNo,
					pageSize: 10,
					perkId:this.id,
					materielCode:this.myFormData.oddNumber
				}
				this.$http.get('storage/mesCertificatePerk/listMesCertificateItemByMainId', {
					params: data
				}).then(res => {
					uni.hideLoading()
					if (res.data.success) {
						this.list = this.list.concat(res.data.result.records);
						this.preservationList = []
						for (let i = 0; i < this.list.length; i++) {
							let data = {
								id: this.list[i].id,
								num: ''
							}
							this.preservationList.push(data)
						}
						if (this.list.length == 0) {
							this.noDataAvailable = true;
						} else if (this.list.length > 0 && res.data.result.records.length == 0) {
							this.noMoreDataAvailable = true;
						} else if (res.data.result.total <= this.pageNo * 10) {
							this.noMoreDataAvailable = true;
						}
						console.log(this.list)
					} else {
						// this.$tip.error(res.data.message)
            this.modal.show({ type:'error', content:res.data.message })
					}
				})
			},
			// 凭证抬头 按钮
			rightClick(){
				this.modalName = 'Modal'
			},
			// 关闭凭证抬头
			close(){
				this.modalName = '';
				this.myModelData.affairContent = '';
			},
			// 保存 凭证抬头  缓存凭证抬头ID
			onSubmit(){
				uni.showLoading({
					mask:true,
					title:'添加凭证抬头中.....'
				})
				if(this.myModelData.affairContent.replace(/\s+/g, "") == ''){
					// this.$tip.error('描述不能为空');
					this.modal.show({ type:'error', content:'描述不能为空' })
					uni.hideLoading()
					return false
				}
				this.$http.post('storage/mesCertificatePerk/add',this.myModelData).then( res =>{
					console.log(res)
					uni.hideLoading()
					if(res.data.success){
						uni.setStorageSync('returnOfAuxiliaryMaterialsRise',res.data.result.id);
						// this.$tip.success(res.data.message)
            this.modal.show({ type:'success', content:res.data.message })
						this.modalName = '';
						this.myModelData.affairContent = '';
					}else {
						// this.$tip.error(res.data.message)
            this.modal.show({ type:'error', content:res.data.message })
					}
				})
			},
			// 退库
			Withdrawal(index){
				if (this.preservationList[index].num == '' || this.preservationList[index].num == 0) {
					// this.$tip.error('退库数量不能为空')
          this.modal.show({ type:'error', content:'退库数量不能为空' })
					return false
				}else if(!uni.getStorageSync('returnOfAuxiliaryMaterialsRise')){
					let date = new Date()
					this.myModelData.cerDate = date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate();
					this.myModelData.accountDate = date.getFullYear() + '-' +(date.getMonth() + 1) + '-' + date.getDate();
					this.myModelData.affairType = uni.getStorageSync('affairType');
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '辅料退库',
					content: '确定退库吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								mask:true,
								title:'退库中.....'
							})
							let data = {
								mobileCode:202,
								mobileType:'辅料退库',
								perkId:uni.getStorageSync('returnOfAuxiliaryMaterialsRise'),
								reserveCode:this.preservationList[index].id,
								inputNum:this.preservationList[index].num
							}
							this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
								console.log(res)
								uni.hideLoading()
								if(res.data.success){
									// this.$tip.success(res.data.message)
                  this.modal.show({ type:'success', content:res.data.message })
								}else {
									// this.$tip.error(res.data.message)
                  this.modal.show({ type:'error', content:res.data.message })
								}
							})
						}
					}
				})
			},
			// 扫描退库
			scanShipment(index){
				if(!uni.getStorageSync('returnOfAuxiliaryMaterialsRise')){
					this.modalName = 'Modal'
					return false
				}
				uni.showModal({
					title: '辅料退库',
					content: '确定扫描退库吗？',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								mask:true,
								title:'扫描退库中.....'
							})
							let data = {
								mobileCode:202,
								mobileType:'扫描辅料退库',
								perkId:uni.getStorageSync('returnOfAuxiliaryMaterialsRise'),
								reserveCode:this.preservationList[index].id,
								inputNum:this.preservationList[index].num
							}
							this.$http.post('storage/mesCertificatePerk/addMesCertificateItem',data).then( res =>{
								console.log(res.data.result.id)
								if(res.data.success){
									uni.setStorageSync('returnOfAuxiliaryMaterialsDetailId',res.data.result.id)
									this.$Router.push({
										name: 'returnOfAuxiliaryMaterialsDetailSaoMiao'
									})
								}else {
									// this.$tip.error(res.data.message)
                  this.modal.show({ type:'error', content:res.data.message })
								}
							})
						}
					}
				})
			},
		},
	}
</script>

<style lang="less">
	page {
		background-color: #f1f1f1;
	
		.fixed {
			position: fixed;
			z-index: 1;
			width: 100%;
			background-color: white;
			height: 100rpx;
			border-bottom: 1rpx solid #eee;
	
			.last {
				border-bottom: 1rpx solid #eee;
				position: relative;
	
				.icon {
					width: 30rpx;
					height: 29rpx;
					position: absolute;
					top: 36rpx;
					left: 60rpx;
				}
	
				input {
					width: 686rpx;
					height: 68rpx;
					background: #FFFFFF;
					border: 1px solid #D5D8DF;
					border-radius: 34px;
					padding-left: 79rpx;
				}
			}
		}
	
		.list {
			margin-top: 120rpx;
	
			.card {
				margin: 30rpx;
				box-shadow: 0px 0px 5rpx 2rpx rgba(0, 0, 0, 0.1);
				border-radius: 10rpx;
				background-color: white;
	
				.top {
					display: flex;
					align-items: center;
					justify-content: space-between;
					font-weight: bold;
					padding: 20rpx 30rpx;
	
					.left {
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: 400;
						color: #485A75;
						line-height: 50rpx;
					}
	
					.right {
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: bold;
						color: #191919;
						line-height: 50rpx;
					}
				}
	
				.line {
					width: 750rpx;
					height: 10rpx;
					background: #F3F5F9;
				}
	
				.bottom {
					padding: 20rpx 30rpx;
	
					.box {
						display: flex;
						justify-content: space-between;
						padding: 20rpx 0;
						align-items: center;
	
						.lable {
							color: #485A75;
							font-size: 28rpx;
						}
	
						.content {
							width: 70%;
							text-align: right;
							color: #191919;
							font-weight: bold;
              word-break: break-all;
						}
	
						.shrink {
							height: 40rpx;
							overflow: hidden;
							white-space: nowrap;
							/*不换行*/
							text-overflow: ellipsis;
							/*超出部分文字以...显示*/
							font-weight: 400;
						}
	
						.unfold {
							text-align: left;
							font-weight: 400;
						}
					}
				}
	
				.btns {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 0 20rpx;
					font-size: 25rpx;
					padding: 20rpx 30rpx;
	
					view {
						width: 180rpx;
						height: 64rpx;
						line-height: 50rpx;
						color: white;
						border-radius: 30rpx;
						padding: 10rpx 20rpx;
						text-align: center;
					}
	
					.btn1 {
						background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
					}
	
					.btn2 {
						background-image: radial-gradient(circle, #716BA9, #362D85, #57509A);
					}
	
					.btn3 {
						background-image: radial-gradient(circle, #354D9E, #354D9E, #697BB7);
					}
				}
			}
		}
	}
	.cu-btn{
		color: white;
		background-image: radial-gradient(circle, #3F55E9, #5F71ED, #7D8CF0);
	}
	.none .none1 {
		font-size: 30rpx;
		text-align: center;
		margin-top: 50rpx;
		color: #999999;
	}
	
	.none2 {
		font-size: 30rpx;
		text-align: center;
		margin-top: 50rpx;
		color: #999999;
	}
	.numName{
		font-weight: bold;
		padding: 10rpx;
		border-radius: 10rpx;
		color: white !important;
		width: 150rpx !important;
	}
	.numInput input{
		margin-left: 44%;
	}
	input {
		border: 2rpx solid #F0F0F0;
		height: 60rpx;
		padding: 10rpx;
		border-radius: 10rpx;
		font-size: 20rpx;
	}
</style>
